<template>
  <div class="admin-container">
    <el-container>
      <el-header>
        <div class="header-content">
          <h2>博客后台管理系统</h2>
          <el-button type="text" class="white" @click="handleLogout">退出登录</el-button>
        </div>
      </el-header>
      
      <el-container>
        <el-aside width="200px">
          <el-menu
            default-active="1"
            class="el-menu-vertical"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b">
            <el-menu-item index="1">
              <i class="el-icon-document"></i>
              <span>文章管理</span>
            </el-menu-item>
            <el-menu-item index="2">
              <i class="el-icon-setting"></i>
              <span>系统设置</span>
            </el-menu-item>
          </el-menu>
        </el-aside>
        
        <el-main>
          <router-view v-if="$route.path !== '/admin'"></router-view>
          
          <el-card v-else>
            <div slot="header" class="header-with-button">
              <span>文章管理</span>
              <el-button type="primary" size="small" @click="createArticle">新增文章</el-button>
            </div>
            
            <el-table :data="articles" v-loading="loading" style="width: 100%">
              <el-table-column prop="title" label="标题" width="250"></el-table-column>
              <el-table-column prop="author.username" label="作者" width="120"></el-table-column>
              <el-table-column prop="createdAt" label="创建时间" width="180">
                <template slot-scope="scope">
                  {{ formatDate(scope.row.createdAt) }}
                </template>
              </el-table-column>
              <el-table-column label="操作" width="200">
                <template slot-scope="scope">
                  <el-button type="text" size="small" @click="viewArticle(scope.row.id)">查看</el-button>
                  <el-button type="text" size="small" @click="editArticle(scope.row.id)">编辑</el-button>
                  <el-button type="text" size="small" @click="confirmDelete(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-card>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'Admin',
  data() {
    return {
      articles: [],
      loading: false
    }
  },
  created() {
    if (this.$route.path === '/admin') {
      this.fetchArticles()
    }
  },
  methods: {
    handleLogout() {
      localStorage.removeItem('token')
      localStorage.removeItem('userInfo')
      this.$message.success('已退出登录')
      this.$router.push('/login')
    },
    fetchArticles() {
      this.loading = true
      this.$axios.get('/api/posts')
        .then(response => {
          this.articles = response.data
          this.loading = false
        })
        .catch(error => {
          console.error('获取文章列表失败:', error)
          this.$message.error('获取文章列表失败，请稍后重试')
          this.loading = false
        })
    },
    formatDate(dateString) {
      if (!dateString) return '未知时间'
      const date = new Date(dateString)
      return date.toLocaleDateString('zh-CN')
    },
    createArticle() {
      this.$router.push('/posts/create')
    },
    viewArticle(articleId) {
      this.$router.push(`/posts/${articleId}`)
    },
    editArticle(articleId) {
      this.$router.push(`/posts/edit/${articleId}`)
    },
    confirmDelete(articleId) {
      this.$confirm('确定要删除这篇文章吗？此操作不可逆', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteArticle(articleId)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    deleteArticle(articleId) {
      this.$axios.delete(`/api/posts/${articleId}`)
        .then(() => {
          this.$message({
            type: 'success',
            message: '文章删除成功'
          })
          this.fetchArticles()
        })
        .catch(error => {
          console.error('删除文章失败:', error)
          this.$message({
            type: 'error',
            message: '删除文章失败，请稍后重试'
          })
        })
    }
  }
}
</script>

<style scoped>
.admin-container {
  height: 100vh;
}

.el-header {
  background-color: #545c64;
  color: white;
  line-height: 60px;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-aside {
  background-color: #545c64;
}

.el-menu-vertical {
  border-right: none;
}

.header-with-button {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-main {
  background-color: #f5f7fa;
  padding: 20px;
}

.el-card {
  margin-bottom: 20px;
  border-radius: 8px;
}

.el-table {
  margin-top: 20px;
}

.el-table th {
  background-color: #f5f7fa;
  color: #606266;
}

.el-button--text {
  color: #409EFF;
  padding: 0 8px;
}

.el-button--text.white {
  color: white;
}
</style>